local skel = fk.CreateSkill {
  name = "rmt__jiuguanshiyi",
}

skel:addEffect("viewas", {
  pattern = ".",
  prompt = "#rmt__jiuguanshiyi",
  interaction = function (self, player)
    local all_choices = player:getTableMark("@$rmt__jiuguanshiyi")
    local choices = player:getViewAsCardNames(skel.name, all_choices)
    if #choices > 0 then
      return UI.CardNameBox {
        choices = choices, all_choices = all_choices,
        default_choice = skel.name,
      }
    end
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 or Fk.all_card_types[self.interaction.data] == nil then return end
    local c = Fk:cloneCard(self.interaction.data)
    c.skillName = skel.name
    c:addSubcard(cards[1])
    return c
  end,
  enabled_at_play = function(self, player)
    return #player:getTableMark("@$rmt__jiuguanshiyi") > 0
  end,
  enabled_at_response = function (self, player, response)
    if not response and Fk.currentResponsePattern and not player:isNude() then
      return #player:getViewAsCardNames(skel.name, player:getTableMark("@$rmt__jiuguanshiyi")) > 0
    end
  end,
})

local function getMoveTypes(dat, player)
  local ret = {}
  for _, move in ipairs(dat) do
    if move.to == player and move.toArea == Card.PlayerHand then
      table.insertIfNeed(ret, "get")
    end
    if move.from == player and table.find(move.moveInfo, function(info)
      return info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip
    end) then
      table.insertIfNeed(ret, "lose")
    end
  end
  return ret
end

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local current_event = room.logic:getCurrentEvent():findParent(GameEvent.MoveCards, true)
    if current_event == nil then return end
    local current_type = getMoveTypes(data, player)
    if #current_type == 0 then return end
    if table.contains(current_type,"lose") then
      room:setPlayerMark(player,"rmt__jiuguanshiyi_tore",0)
      room:setPlayerMark(player,"@$rmt__jiuguanshiyi",0)
      room:setPlayerMark(player,"rmt__jiuguanshiyi_canre",1)
    end
    if table.contains(current_type,"get") then
      if #player:getTableMark("rmt__jiuguanshiyi_tore")>0 then
      room:setPlayerMark(player,"@$rmt__jiuguanshiyi", player:getTableMark("rmt__jiuguanshiyi_tore"))
      end
    end
  end,
})

skel:addEffect(fk.CardUseFinished, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(skel.name) then
      return player:getMark("rmt__jiuguanshiyi_canre") ~= 0 and data.card and data.card.type ~= Card.TypeEquip
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark("rmt__jiuguanshiyi_tore")
    table.insertIfNeed(mark, data.card.name)
    room:setPlayerMark(player, "rmt__jiuguanshiyi_tore", mark)
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@$rmt__jiuguanshiyi", 0)
end)

Fk:loadTranslationTable{
  ["rmt__jiuguanshiyi"] = "旧馆拾忆",
  [":rmt__jiuguanshiyi"] = "你可以将一张牌当你上次失去牌与上次获得牌之间被使用过的一张非装备牌使用。",
  ["@$rmt__jiuguanshiyi"] = "旧馆拾忆",
  ["#rmt__jiuguanshiyi"] = "旧馆拾忆：选择一个牌名，将一张牌转化为之使用",
}

return skel
